package com.partsoft.spring.webapp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ContextLoaderServlet extends HttpServlet {
	
	private static final long serialVersionUID = 5318615178409752416L;
	
	public static final String ARG_WEB_SPRINGFILE = "springfilelocation";
	
	private ContextLoader contextLoader;
	
	public void init() throws ServletException {
		this.contextLoader = createContextLoader();
		this.contextLoader.setServletConfigLocationParameter(getInitParameter(ContextLoader.CONFIG_LOCATION_PARAM));
		this.contextLoader.initWebApplicationContext(getServletContext());
	}

	/**
	 * 创建ApplicationContext
	 * @return
	 */
	protected ContextLoader createContextLoader() {
		return new ContextLoader();
	}
	
	
	public ContextLoader getContextLoader() {
		return this.contextLoader;
	}

	public void destroy() {
		if (this.contextLoader != null) {
			this.contextLoader.closeWebApplicationContext(getServletContext());
		}
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		getServletContext().log("Attempt to call service method on DitsContextLoaderServlet as [" + request.getRequestURI() + "] was ignored");
		response.sendError(HttpServletResponse.SC_BAD_REQUEST);
	}

	public String getServletInfo() {
		return "ContextLoaderServlet for Servlet API 2.3 " + "(deprecated in favor of ContextLoaderListener for Servlet API 2.4)";
	}
}
